//
//  ZqlStatusCell.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/21.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit

class ZqlStatusCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    //添加约束对象属性
    var bottomConstraint:Constraint?
    
    //添加模型属性
    var model:ZqlStatusModel?{
        didSet{
            //给原创微博设置数据模型
            OriginalView.model = model
            
            //卸载原有约束
            self.bottomConstraint?.uninstall()
            if let ret = model?.retweeted_status{
                //给转发微博设置数据模型
                RetweetedView.retweetedStatus = ret
                bottomView.snp_makeConstraints { (make) -> Void in
                    //添加约束对象 并且记录约束对象
                    self.bottomConstraint =  make.top.equalTo(RetweetedView.snp_bottom).constraint
                }
                
                //显示转发微博视图
                RetweetedView.hidden = false
            }else{
                //原创微博
                bottomView.snp_makeConstraints { (make) -> Void in
                    //添加约束对象 并且记录约束对象
                    self.bottomConstraint =  make.top.equalTo(OriginalView.snp_bottom).constraint
                }
                //隐藏转发微博视图
                RetweetedView.hidden = true
                
            }
            
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpCell()
        //设置cell的选中样式
        selectionStyle = .None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //懒加载子视图
    private lazy var OriginalView:ZqlOriginalView = ZqlOriginalView()
    //懒加载底部视图
    private lazy var bottomView:ZqlBottomView = ZqlBottomView()
    //懒加载转发微博视图
    private lazy var RetweetedView:ZqlRetweetedView = ZqlRetweetedView()
    
    //添加视图
    private func setUpCell(){
        
        //将原创view添加到cell上
        contentView.addSubview(OriginalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(RetweetedView)
        
        //添加约束
        OriginalView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
        }
        
        //底部视图约束
        bottomView.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint = make.top.equalTo(RetweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(35)
        }
        
        //转发微博约束
        RetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(OriginalView.snp_bottom)
            make.left.equalTo(OriginalView.snp_left)
            make.right.equalTo(OriginalView.snp_right)
        }

        
        //给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    
    
}
